/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function parse(form) {
    var string = form.command.value;
    var panjang = string.length; //panjang input
    var i = 0; //iterator
    var karakter = string.charAt(i); //karakter
    if (karakter == '/') {
        var command = karakter;
        while (i<panjang && karakter != ' ') { //membaca perintah pertama
            i++;
            karakter = string.charAt(i);
            if (karakter != ' ') { //menghindari pembacaan spasi
                command = command + karakter;
            }
        }
        i++;
        karakter = string.charAt(i); //membaca karakter selanjutnya

        var parameter = karakter;
        while (i<panjang && karakter != ' ') { //membaca parameter
            i++;
            karakter = string.charAt(i);
            if (karakter != ' ') { //menghindari pembacaan spasi
                parameter = parameter + karakter;
            }
        }

        i++;
        karakter = string.charAt(i); //membaca karakter selanjutnya

        var parameter2 = karakter;
        while (i<panjang && karakter != ' ') { //membaca parameter
            i++;
            karakter = string.charAt(i);
            if (karakter != ' ') { //menghindari pembacaan spasi
                parameter2 = parameter2 + karakter;
            }
        }
		//berikut ini cuma buat ngetes doang komennya masuk apa ga

        if (command == "/create") {
            alert(command + " " + parameter + " " + parameter2);
            var url = "Users?" + "action=createroom&name=" + parameter + "&category=" + parameter2;
            biji.load(url, function(data) {
                    var json = eval('(' + data + ')');
                    
                    var room = "";
                    var idcategory = -1;
                    for (var i in json.room)
                    {
                        alert(json.room[i].keyroom);
                    }
                });
        } else
        if (command == "/join") {
            alert(command +" "+parameter);
        } else
        if (command == "/chat") {
            alert(command +" "+parameter);
        } else
        if (command == "/addfriend") {
            alert(command +" "+parameter);
        } else
        if (command == "/exit") {
            alert(command +" "+parameter);
        } else
        if (command == "/info") {
            alert(command +" "+parameter);
        } else
        if (command == "/kick") {
            alert(command +" "+parameter);
        } else
        if (command == "/ban") {
           alert(command +" "+parameter);
        } else
        if (command == "/unban") {
            alert(command +" "+parameter);
        } else
        if (command == "/persist") {
            alert(command +" "+parameter);
        } else
        if (command == "/setowner") {
            alert(command +" "+parameter);
        } 
    }else{
            alert("ini bukan command , ini pesan : " + string);
            var command = string;
            var recipient = form.recipient.value;
            var type = "2"; // private

            var index = tabview.getTabIndexByTitleDirect(recipient);

            if (recipient[0] == "#")
            {
                type = "1"; // room
                recipient = recipient.substring(1);
            }
            // parser

            var content = escape(command);
            var url = "Dispatcher?" + "action=post&type=" + type + "&recipient=" + recipient + "&content=" + content;
            alert(url);
            biji.load(url, function() {}, "post");
            form.command.value = "";
    }
    return false;
}

function send_command(form) {
    var command = form.command.value;
    var recipient = form.recipient.value;
    var type = "2"; // private

    var index = tabview.getTabIndexByTitleDirect(recipient);

    if (recipient[0] == "#")
    {
        type = "1"; // room
        recipient = recipient.substring(1);
    }
    // parser
    var content = escape(command);
    var url = "Dispatcher?" + "action=post&type=" + type + "&recipient=" + recipient + "&content=" + content;
    alert(url);
    biji.load(url, function() {}, "post");
    form.command.value = "";

    return false;
}


function refreshcategory(){
    var url = "Dispatcher?" + "action=refreshcategory";

    biji.load(url, function(data) {
        var json = eval('(' + data + ')');

        biji.select("#treemenu").innerHTML = "";
        var category = "";
        for (var i in json.category)
        {
            category += "<li>" + json.category[i].keycategory + "<ul>";
            for(var j in json.category[i].arraysubcategory)
            {
                category += "<li>" + json.category[i].arraysubcategory[j].keysubcategory + "<ul id=treeroom"+ json.category[i].arraysubcategory[j].valsubcategory +"></ul></li>" ;
            }
            category += "</ul></li>";
        }
        biji.select("#treemenu").innerHTML = category;
        ddtreemenu.createTree("treemenu", true);
    });
}